package com.cskaoyan.service.back;

import com.cskaoyan.bean.po.BasePageInfo;
import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.po.MarketLog;
import com.cskaoyan.bean.po.MarketLogExample;
import com.cskaoyan.mapper.MarketLogMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 12-江涛
 * @since 2022/10/13  0:37
 */
@Service
public class LogServiceImpl implements LogService {

    @Autowired
    MarketLogMapper marketLogMapper;

    @Override
    public CommonData<MarketLog> logList(BasePageInfo basePageInfo,String name) {

        //查表
        MarketLogExample marketLogExample = new MarketLogExample();
        MarketLogExample.Criteria criteria = marketLogExample.createCriteria();

        if (name != null) {

            criteria.andAdminEqualTo(name);
        }

        criteria.andDeletedEqualTo(false);
        List<MarketLog> marketLogs = marketLogMapper.selectByExample(marketLogExample);

        //分页
        PageHelper.startPage(basePageInfo.getPage(),basePageInfo.getLimit());
        marketLogExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());

        PageInfo<MarketLog> marketLogPageInfo = new PageInfo<>(marketLogs);

        CommonData<MarketLog> commonData = new CommonData<>();
        commonData.setList(marketLogs);
        commonData.setPages(marketLogPageInfo.getPages());
        commonData.setPage(marketLogPageInfo.getPageNum());
        commonData.setTotal((int) marketLogPageInfo.getTotal());
        commonData.setLimit(basePageInfo.getLimit());

        return commonData;
    }

    @Override
    public void insert(MarketLog marketLog) {
        marketLogMapper.insertSelective(marketLog);
    }
}
